#include "additionalability.h"
#include <QDebug>
#include <QRegExp>

AdditionalAbility::AdditionalAbility(QObject *parent) :
    QObject(parent)
{
    m_Price = "";
    m_Ability = "";
}

void AdditionalAbility::setPrice(QString newPrice)
{
    m_Price = newPrice;
}

QString AdditionalAbility::getPrice()
{
    return m_Price;
}

void AdditionalAbility::setAbility(QString newAbility)
{    
    m_Ability = newAbility;
}

QString AdditionalAbility::getAbility()
{
    return m_Ability;
}

void AdditionalAbility::Init(QString newPrice, QString newAbility)
{
    setPrice(newPrice);
    setAbility(newAbility);
}

void AdditionalAbility::Init(QString domData)
{
    int priceBegin = domData.indexOf("<Price>");
    int abilityBegin = domData.indexOf("<Ability>");

//    QDomDocument *doc = new QDomDocument();
//    doc->setContent(domData);
//    QDomElement addAbility = doc->firstChild().toElement();
//
//    QDomNode price = addAbility.elementsByTagName(WORD_PRICE).item(0);

    if ((domData.indexOf("<Price/>") == -1) && (domData.indexOf("<Price>") != -1))
        setPrice(domData.mid(priceBegin, domData.indexOf("</Price") - priceBegin + ((QString)"</Price").length()));
    if ((domData.indexOf("<Ability/>") == -1) && (domData.indexOf("<Ability>") != -1))
        setAbility(domData.mid(abilityBegin, domData.indexOf("</Ability") - abilityBegin + ((QString)"</Ability").length()));
}

QString AdditionalAbility::toText(bool withoutBR)
{
    QString result = "<AdditionalAbility>" + m_Price + "<Ability" +
            ((m_Ability.length() > 0)?(">" + m_Ability  + "</Ability>"):("/>")) + "</AdditionalAbility>";
    if (withoutBR)
        result.remove("\n");
    return result;
}
